﻿#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui,QtWidgets
from ui_setting import Ui_DlgSettingClass
from PyQt5.QtWidgets import  QFileDialog
import config_json
import os
class DlgSetting(QtWidgets.QDialog):
    def __init__(self, parent=None):
        logging.info("init")
        super(DlgSetting, self).__init__(parent)
        self.ui=Ui_DlgSettingClass()
        self.ui.setupUi(self)
        # self.ui.frame.setVisible(False)
        database_path=config_json.setting2.value("database")
        self.ui.lineEdit.setText(database_path)
        output_folder=config_json.setting2.value("output_folder")
        self.ui.lineEdit_2.setText(output_folder)
        yqbh=str(config_json.setting2.value("yqbh"))
        self.ui.lineEdit_yqbh.setText(yqbh)
        # self.ui.label_sblx.setText(config_json.setting["sblx"])
        at=0
        ii=0
        sblx=config_json.setting2.value("sblx")
        for i in config_json.setting["sblx_list"]:
            if i== sblx:
                at=ii
            self.ui.cmb_sblx.addItem(i)
            ii+=1
        self.ui.cmb_sblx.setCurrentIndex(at)
        self.ui.cmb_sblx.setEditable(True)

        self.ui.cmb_sblx.currentIndexChanged[int].connect(self.sblx_change)
        self.ui.btnSelect.clicked.connect(self.select)
        self.ui.btnOutput.clicked.connect(self.select2)
    def sblx_change(self,at):
        # self.ui.label_sblx.setText(self.ui.cmb_sblx.itemText(at));
        pass
    def select(self):
        FileDialog = QFileDialog(self)
        FileDirectory = FileDialog.getOpenFileName(self,'select database file')                #选择目录，返回选中的路径
        logging.info(FileDirectory)
        self.ui.lineEdit.setText(FileDirectory[0])
    def select2(self):
        FileDialog = QFileDialog(self)
        FileDirectory = FileDialog.getExistingDirectory(self,'select output folder')                #选择目录，返回选中的路径
        logging.info(FileDirectory,type(FileDirectory),dir(FileDirectory))
        if FileDirectory!="":
            self.ui.lineEdit_2.setText(FileDirectory)
        else:
            logging.info("not select")
    #@QtCore.pyqtSlot()
    def accept(self):
        logging.info("accept")
        config_json.setting2.setValue('database',self.ui.lineEdit.text())
        config_json.setting2.setValue('output_folder',self.ui.lineEdit_2.text())
        config_json.setting2.setValue('yqbh',self.ui.lineEdit_yqbh.text())
        # config_json.setting['sblx']=self.ui.cmb_sblx.currentText()
        config_json.setting2.setValue("sblx",self.ui.cmb_sblx.currentText())
        if self.ui.cmb_sblx.currentText() in ["uni_cs_msg","uni_on_msg"]:
            self.output_myconfig(self.ui.lineEdit.text())
            pass #save myconfig
        self.done(1)
        pass
    def output_myconfig(self,database_path):
        ps=database_path.split("/")
        r=""
        for i in ps[:-3]:
            r=r+i+"\\"
        f=open("myconfig.py","w",encoding="utf-8")
        f.write("""uni_bin_path=r'%s'
uni_db_path=r'%s'""" %(os.path.join(r,"Bin"),database_path.replace("/","\\")))
        f.close()
    def reject(self):
        logging.info("reject")
        self.done(0)
        pass
def main():        
    import sys
    app = QtWidgets.QApplication(sys.argv)
    calculator = DlgSetting()
    #calculator.showdata(9)
    #calculator.show()
    calculator.exec()
    #sys.exit(app.exec_())

if __name__ == '__main__':
    main()
